package com.conan.controller;

import com.conan.pojo.Result;
import com.conan.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> fileUpload(MultipartFile file) throws Exception {
        //把文件内容存储到本地磁盘

        //获取文件初始名称
        String originalFilename = file.getOriginalFilename();

        //保证文件名唯一
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //file.transferTo的异常直接抛出去
        //再次存储的时候使用原来的文件名
//        file.transferTo(new File("C:\\Users\\Conan\\Desktop\\files\\"+filename));

        //uploadFile()方法上异常直接抛出即可
        String url = AliOssUtil.upLoadFile(filename,file.getInputStream());
        return Result.success(url);
    }
}
